package com.yylending.las.deviceinfo;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.yyfax.common.CLog;
import com.yylending.las.Constants;

import java.util.List;

import rx.Observable;

/**
 * Created by laixj on 2017/7/21.
 * Copyright ©2017 www.yylending.com. All Rights Reserved.
 */

public class UploadDeviceService extends IntentService implements UploadDevicePresenter.View {

	private static final String TAG = "UploadDeviceService";

	private UploadDevicePresenter presenter;

	public UploadDeviceService() {
		super(TAG);
		presenter = new UploadDevicePresenter();
		presenter.attach(this);
		CLog.i(TAG, "onHandleIntent UploadDeviceService 构造");
	}

	@Override
	protected void onHandleIntent(@Nullable Intent intent) {
		String evalPath = intent.getStringExtra(Constants.EVALPATH);
		String longitude = intent.getStringExtra(Constants.LONGITUDE);
		String latitude = intent.getStringExtra(Constants.LATITUDE);
		CLog.i(TAG, "onHandleIntent evalPath=" + evalPath);
		CLog.i(TAG, "onHandleIntent longitude=" + longitude);
		CLog.i(TAG, "onHandleIntent latitude=" + latitude);
		if (Constants.EVALPATH_CALL.equals(evalPath)) {
			List<UploadDeviceModel.CallLogs> callLogsList = UploadDeviceModel.getCallLogs(this);
			if (callLogsList.size() > 0) {
				presenter.uploadCalLogs(new UploadDeviceModel.UploadCallLog(longitude, latitude, callLogsList));
			}
		} else if (Constants.EVALPATH_SMS.equals(evalPath)) {
			List<UploadDeviceModel.SmsInfo> smsList = UploadDeviceModel.getSmsInfo(this);
			if (smsList.size() > 0) {
				presenter.uploadSmsInfo(new UploadDeviceModel.UploadSms(longitude, latitude, smsList));
			}
		} else if (Constants.EVALPATH_SOFTWARE.equals(evalPath)) {
			List<UploadDeviceModel.AppInfo> appList = UploadDeviceModel.getAppInfo(this);
			if (appList.size() > 0) {
				presenter.uploadAppInfo(new UploadDeviceModel.UploadApp(longitude, latitude, appList));
			}
		} else if (Constants.EVALPATH_CONTACT.equals(evalPath)) {
			List<UploadDeviceModel.ContactInfo> contactList = UploadDeviceModel.getContacts(this);
			if (contactList.size() > 0) {
				presenter.uploadContacts(new UploadDeviceModel.UploadContact(longitude, latitude, contactList));
			}
		}
	}

	@Override
	public void onDestroy() {
		presenter.detach();
		CLog.i(TAG, "onHandleIntent UploadDeviceService onDestroy");
		super.onDestroy();
	}

	@Override
	public void uploadSuccess(String evalPath, String result) {
		CLog.i(TAG, evalPath + " uploadSuccess");
	}

	@Override
	public void onFunctionStart(boolean autoTips) {

	}

	@Override
	public void onFunctionCompleted(boolean autoTips) {

	}

	@Override
	public void onFunctionError(Throwable e, boolean autoTips, int functionalCode) {

	}

	@Override
	public void uploadFail(String evalPath, String result) {
		CLog.i(TAG, evalPath + " uploadFail");
	}

	@Override
	public Observable.Transformer composeLifecycle() {
		return null;
	}
}
